import { z } from "zod";
import { codeartsrepoRequest } from "../common/utils.js";

// https账号密码校验
export const ValidateHttpsInfoV2Schema = z.object({
  username: z.string().describe("用户名"),
  password: z.string().describe("密码"),
});

// 功能实现
export async function validateHttpsInfoV2(
  username: string,
  password: string
) {
  try {
    const response = await codeartsrepoRequest(`https://codeartsrepo.example.com/api/v2/users/validate`, {
      method: "POST",
      body: {
        username,
        password
      },
      headers: {
        // 这里不使用全局的认证信息，而是使用提供的用户名和密码进行Basic认证
        "Authorization": `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`
      }
    });
    
    return z.object({
      valid: z.boolean(),
      user_id: z.string().optional(),
    }).parse(response);
  } catch (error) {
    // 认证失败时直接返回无效
    return {
      valid: false
    };
  }
}

// 获取当前用户信息
export async function getCurrentUser() {
  const response = await codeartsrepoRequest("https://codeartsrepo.example.com/api/v2/users/current");
  
  return z.object({
    id: z.string(),
    name: z.string(),
    email: z.string(),
    avatar_url: z.string().optional(),
    created_at: z.string(),
    updated_at: z.string(),
  }).parse(response);
} 